package com.youan.log.modules.audit.dataimport;

import org.springblade.core.tool.utils.SpringUtil;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

public class TableCreator {

    private String tableName;
    private JdbcTemplate jdbcTemplate;

    public TableCreator(String date) {
        this.tableName = "region_data_" + date.replaceAll("-", "_");
        DataSource dataSource = SpringUtil.getBean(DataSource.class);
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }


    public void create() {
        StringBuilder sql = new StringBuilder();
        sql.append("CREATE TABLE `log_audit_system`.`").append(tableName).append("`  (")
                .append("  `id` bigint NOT NULL,")
                .append("   `direction` varchar(10) NULL COMMENT '方向(s/d)',")
                .append("   `rule_id` varchar(30) NULL COMMENT '规则id',")
                .append("   `source_ip` varchar(30) NULL COMMENT '源ip',")
                .append("   `destination_ip` varchar(30) NULL COMMENT '目的IP',")
                .append("   `source_port` varchar(30) NULL COMMENT '源端口',")
                .append("   `destination_port` varchar(30) NULL COMMENT '目的端口',")
                .append("   `date` varchar(30) NULL,")
                .append("   `time` varchar(30) NULL,")
                .append("   `payload` text NULL,")
                .append("   `login_failure_account` varchar(255) NULL COMMENT '登陆失败访问账户',")
                .append("   PRIMARY KEY (`id`)")
                .append(" )");
        this.jdbcTemplate.execute(sql.toString());
    }




}
